package com.tsmask;

import java.util.Timer;
import java.util.TimerTask;

import javax.swing.SwingUtilities;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tsmask.bound.CityBound;
import com.tsmask.layout.MainJFrame;

/**
 * 主入口
 * 
 * @author TsMask
 *
 */
public class Main {

	// 省市数组
	public static JSONArray provinceArray;
	// 城市数组
	public static JSONArray cityArray;
	// 区域数组
	public static JSONArray townArray;
	// 天气信息
	public static JSONObject weatherInfo;

	public static void main(String[] args) {
		HttpUtil httpUtil = new HttpUtil();
		// 获取当前位置天气信息
		httpUtil.clockWeather();
		//
		JSONArray jsonArray = Main.weatherInfo.getJSONArray("area");
		// 全部省列表
		httpUtil.areaQuery("province", null);
		for (Object province : Main.provinceArray) {
			JSONArray provinceArray = (JSONArray) province;
			CityBound.provinceJComboBox.addItem(provinceArray.getString(0));
		}
		CityBound.provinceJComboBox.setSelectedItem(jsonArray.getJSONArray(0).getString(0));
		// 城市数据
		httpUtil.areaQuery("city", jsonArray.getJSONArray(0).getString(1));
		for (Object city : Main.cityArray) {
			JSONArray cityArr = (JSONArray) city;
			CityBound.cityJComboBox.addItem(cityArr.getString(0));
		}
		CityBound.cityJComboBox.setSelectedItem(jsonArray.getJSONArray(1).getString(0));
		// 区域数据
		httpUtil.areaQuery("town", jsonArray.getJSONArray(1).getString(1));
		for (Object town : Main.townArray) {
			JSONArray townArr = (JSONArray) town;
			CityBound.townJComboBox.addItem(townArr.getString(0));
		}
		CityBound.townJComboBox.setSelectedItem(jsonArray.getJSONArray(2).getString(0));
		// 请求天气详情
		httpUtil.weatherQuery(jsonArray.getJSONArray(2).getString(1));

		// 延迟2秒显示
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				// 使用SwingUtilities工具类调用MainView主布局CreateAndShowGUI方法显示
				SwingUtilities.invokeLater(MainJFrame::CreateAndShowGUI);
			}
		};
		timer.schedule(task, 2000);

	}

}
